package com.bdqn.controller;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSONObject;
import com.bdqn.pojo.AppInfo;
import com.bdqn.service.AppInfoService;
import com.bdqn.util.LayData;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;

@Controller
@RequestMapping("/backend")
public class BackendController extends BaseController{

    @Resource
    private AppInfoService appInfoService;

    @RequestMapping(value="/tolist",method = RequestMethod.GET)
    public String tolist(){
        return "jsp/backend/list";
    }

    @RequestMapping(value="/toaddus",method = RequestMethod.GET)
    public String toadduser(){
        return "jsp/dev/add";
    }

    @RequestMapping(value = "/upload")
    @ResponseBody
    public String addphone(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName =new Date().getTime()+file.getOriginalFilename().toString();
        FileUtil.writeBytes(file.getBytes(),"C:/Users/student/IdeaProjects/App/src/main/webapp/static/images/"+fileName);
        return JSONObject.toJSONString(LayData.data_upload(1L,fileName));
    }


    @RequestMapping(value = "/shen")
    /*@PathVariable("id")*/
    @ResponseBody
    public Long touupdate(@RequestParam(value="nna",required=false)Integer nna) throws IOException {

        return  appInfoService.updZhuang(6l,nna) ;
    }

    @RequestMapping(value = "/shen1")
    /*@PathVariable("id")*/
    @ResponseBody
    public Long touupdate1(@RequestParam(value="nna",required=false)Integer nna) throws IOException {

        return  appInfoService.updZhuang(8l,nna) ;
    }


    @RequestMapping(value="/list",method = RequestMethod.GET)
    @ResponseBody
    public String getUserList(@RequestParam("page") Long from, @RequestParam("limit") Long limit, @RequestParam(value="name",required=false) String name, @RequestParam(value="zhuang",required=false) Long zhuang
            , @RequestParam(value="ping",required=false) Long ping, @RequestParam(value="one",required=false) Long one, @RequestParam(value="two",required=false) Long two, @RequestParam(value="three",required=false) Long three){

        LayData data = appInfoService.getAll(name,zhuang,5L,one,two,three,from,limit);
        return JSONObject.toJSONString(data);
    }
}
